import 'core-js/stable'
import 'regenerator-runtime/runtime'

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
import router from './router/index'
import store from './store'
import * as API from './utils/request'
import { Dialog } from 'vant';
// Vue.use(NavBar)

// 设置 js中可以访问 $cdn
import {
  $cdn
} from '@/config'
Vue.prototype.$cdn = $cdn
Vue.prototype.API = API

// 全局引入按需引入UI库 vant
import '@/plugins/vant'
// 引入全局样式
import '@/assets/css/index.scss'
// 移动端适配
import 'lib-flexible/flexible.js'

//全局引入mock
import './utils/mock'//此部分引入的是我们所编写的mockjs文档
import axios from 'axios'
Vue.prototype.$axios = axios


// filters
import './filters'
Vue.config.productionTip = false
// 注册全局
Vue.use(VueRouter)



router.beforeEach((to, from, next) => {
  let userName = store.getters.userName

  if (to.matched.some(url => url.meta.login)) {
    if (to.meta.login && !userName) {
      Dialog.alert({
        title: '提示',
        message: '该模块需要登录，请登录',
      }).then(() => {
        next({ path: '/login' })
      });


    } else {
      next()
    }
  } else {
    next()
  }
})
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }


new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
